package config

import (
	"encoding/json"
	"io/ioutil"
)

type jsonFile struct {
	bytes []byte
	name string
}


func (jf *jsonFile) Read(b []byte) (n int,err error) {
	n = copy(b,jf.bytes)
	return
}

func (jf *jsonFile) Write (b []byte) (n int,err error) {
	jf.bytes = make([]byte,len(b))
	n = copy(jf.bytes,b)
	return
}

func (jf *jsonFile) Unmarshal(v interface{}) (err error){
	err = json.Unmarshal(jf.bytes,v)
	if err != nil {
		return
	}

	return
}

func (jf *jsonFile)Marshal(v interface{}) (err error) {
	jf.bytes,err = json.MarshalIndent(v,"","\t")
	if err != nil {
		return
	}
	err = ioutil.WriteFile(jf.name,jf.bytes,0755)
	return
}
